﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Fashion_API;

namespace Fashion.BackEnd.ManageProduct
{
    public partial class ManageProductProperty : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
            }
        }

        private void BindData()
        {
            DALDataContext d = new DALDataContext();
            ProductCategory productCategory =
                d.ProductCategories.FirstOrDefault(i => i.Id == Convert.ToInt32(A.M.GetQueryString("CategoryId")));
            if (productCategory.ParentId==null)
            {
                litCategoryName.Text = productCategory.Name;
            }
            else
            {
                litCategoryName.Text = productCategory.ProductCategory1.Name + " > " + productCategory.Name;
            }
            grdProductProperty.DataSource =
                A.Controller.ProductPropertyController.GetAllPropertyOfCategory(Convert.ToInt32(A.M.GetQueryString("CategoryId")));
            grdProductProperty.DataBind();
        }

        protected void grdAccount_ItemCommand(object source, DataGridCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "cmdAddNew":
                    using (DALDataContext d = new DALDataContext())
                    {
                        ProductProperty productProperty = new ProductProperty();
                        TextBox txtName = (TextBox) e.Item.FindControl("txtName");
                        DropDownList cboDataType = (DropDownList) e.Item.FindControl("cboDataType");
                        TextBox txtUnit = (TextBox)e.Item.FindControl("txtUnit");
                        CheckBox chkIsMultiValue = (CheckBox)e.Item.FindControl("chkIsMultiValue");
                        CheckBox chkIsValueStatic = (CheckBox)e.Item.FindControl("chkIsValueStatic");
                        productProperty.CategoryId = Convert.ToInt32(A.M.GetQueryString("CategoryId"));
                        productProperty.Name = txtName.Text.Trim();
//                        productProperty.DataType = Convert.ToByte(cboDataType.SelectedValue);
                        productProperty.DataType = 0;
                        productProperty.Unit = txtUnit.Text.Trim();
                        productProperty.IsMultiValue = chkIsMultiValue.Checked;
                        productProperty.IsValueStatic = chkIsValueStatic.Checked;
                        productProperty.IsActive = true;
                        d.ProductProperties.InsertOnSubmit(productProperty);
                        d.SubmitChanges();
                    }
                    break;
                case "cmdUpdate":
                    using (DALDataContext d = new DALDataContext())
                    {
                        ProductProperty productProperty = d.ProductProperties.FirstOrDefault(i => i.Id == Convert.ToInt32(e.CommandArgument));
                        TextBox txtName = (TextBox)e.Item.FindControl("txtName");
                        DropDownList cboDataType = (DropDownList)e.Item.FindControl("cboDataType");
                        TextBox txtUnit = (TextBox)e.Item.FindControl("txtUnit");
                        CheckBox chkIsMultiValue = (CheckBox)e.Item.FindControl("chkIsMultiValue");
                        CheckBox chkIsValueStatic = (CheckBox)e.Item.FindControl("chkIsValueStatic");
                        productProperty.CategoryId = Convert.ToInt32(A.M.GetQueryString("CategoryId"));
                        productProperty.Name = txtName.Text.Trim();
                        productProperty.DataType = 0;
                        productProperty.Unit = txtUnit.Text.Trim();
                        productProperty.IsMultiValue = chkIsMultiValue.Checked;
                        productProperty.IsValueStatic = chkIsValueStatic.Checked;
                        d.SubmitChanges();
                    }
                    break;
                case "cmdDelete":
                    using (DALDataContext d = new DALDataContext())
                    {
                        ProductProperty productProperty = d.ProductProperties.FirstOrDefault(i => i.Id == Convert.ToInt32(e.CommandArgument));
                        d.ProductProperties.DeleteOnSubmit(productProperty);
                        d.SubmitChanges();
                    }
                    break;
                case "cmdIsActive":
                    using (DALDataContext d = new DALDataContext())
                    {
                        ProductProperty productProperty = d.ProductProperties.FirstOrDefault(i => i.Id == Convert.ToInt32(e.CommandArgument));
                        productProperty.IsActive = !productProperty.IsActive;
                        d.SubmitChanges();
                    }
                    break;
            }
            BindData();
        }

        protected void grdCategory_ItemDataBound(object sender, DataGridItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {

            }
            if (e.Item.ItemType == ListItemType.Footer)
            {
                DropDownList cboDataType = (DropDownList)e.Item.FindControl("cboDataType");
                A.M.BindDataComboBoxFromEnum(cboDataType,typeof(A.Enum.DataType));
            }
        }
    }
}